<?php

class Admin_TextsController extends CMS_Controller_Action_Admin {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $langs = new CMS_Models_Lang();
        $this->view->langs =$langs->getEnabledLangs();
        $this->view->texts = $this->formatTexts($this->view->langs);
    }

    public function savetextAction() {
        parent::disableLayout();
        $request = $this->_request->getParams();
        error_reporting(0);
        $result = array();
        $xmlUrl = SYSTEM_PATH . "/library/Common/Translate/translations/default.xml"; // XML feed file/URL
        $dom = new DOMDocument();
        $dom->load($xmlUrl);
        $arrXml = $dom->getElementsByTagName("tu");
        foreach ($arrXml as $a) {
            $attr = $a->getAttribute("tuid");
            if ($attr == $request['key']) {
                $elems = $a->getElementsByTagName("tuv");
                foreach ($elems as $e) {
                    if ($e->getAttribute("xml:lang") == $request['lang']) {
                        $seg = $e->getElementsByTagName("seg");
                        foreach ($seg as $s) {
                            $e->removeChild($s);
                        }
                        $tr = new DOMElement('seg', $request['texto']);
                        $e->appendChild($tr);
                        unlink(SYSTEM_PATH . "/library/Common/Translate/translations/default.xml");
                        $xml = fopen(SYSTEM_PATH . "/library/Common/Translate/translations/default.xml", "a+");
                        fwrite($xml, $dom->saveXML());
                        return;
                    }
                }
                $tr = new DOMElement('seg', $request['texto']);
                $tuv = new DOMElement("tuv");
                $tuv->appendChild($tr);
                $tuv->setAttribute("xml:lang", $request['lang']);
                $a->appendChild($tuv);
                unlink(SYSTEM_PATH . "/library/Common/Translate/translations/default.xml");
                $xml = fopen(SYSTEM_PATH . "/library/Common/Translate/translations/default.xml", "a+");
                fwrite($xml, $dom->saveXML());
                return;
            }
        }
    }

    public function savetextsAction() {
        parent::disableLayout();
        $json = json_decode($_POST['json']);
        $xmlUrl = SYSTEM_PATH . "/library/Common/Translate/translations/default.xml";
        if (is_file($xmlUrl))
            unlink($xmlUrl);
        $file = fopen($xmlUrl, "a+");
        fwrite($file, '<?xml version="1.0" ?><!DOCTYPE tmx ><tmx version="1.4"><header creationtoolversion="1.0.0" datatype="winres" segtype="sentence" adminlang="es-es" srclang="de-at" o-tmf="abc" creationtool="XYZTool" ></header><body>');
        foreach ($json as $j) {
            $langs = MD_Lang::getEnabledLangs();
            fwrite($file, "<tu tuid='" . $j->texto . "'>");
            foreach ($langs as $l) {
                $$lang = $l['prefix2'];
                fwrite($file, '<tuv xml:lang="' . $l['prefix2'] . '"><seg>' . $j->$$lang . '</seg></tuv>');
            }
            fwrite($file, "</tu>");
        }
        fwrite($file, " </body></tmx>");
        fclose($file);
    }

    private function formatTexts($langs) {
        error_reporting(0);
        $result = array();
        $xmlUrl = SYSTEM_PATH . "/library/Common/Translate/translations/default.xml"; // XML feed file/URL
        $dom = new DOMDocument();
        $dom->load($xmlUrl);
        $arrXml = $dom->getElementsByTagName("tu");
        foreach ($arrXml as $a) {
            $attr = $a->getAttribute("tuid");
            $tr = array();
            $tr['texto'] = $attr;
            foreach ($langs as $l) {
                $tr[$l['prefix2']] = Common_Translate::getTranslateOf($attr, $l['prefix2']);
            }
            array_push($result, $tr);
        }

        return $result;
    }

}
